package com.magicsoft.share.presenter.exchange;

import com.magicsoft.share.base.BasePresenter;
import com.magicsoft.share.bean.exchange.Freight;
import com.magicsoft.share.bean.exchange.ShopCartGoods;
import com.magicsoft.share.constant.APIService;
import com.magicsoft.share.constant.SPKey;
import com.magicsoft.share.rx_retrofit_network.location.retrofit.RetrofitUtil;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.LoadingTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.NetWorkTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.SimpleObserver;
import com.magicsoft.share.utils.SPUtils;
import com.magicsoft.share.view.exchange.IShopCartView;

import java.util.List;

import io.reactivex.annotations.NonNull;

/**
 * Created by lyf on 2017/12/1/001.
 */

public class ShopCartPresenter extends BasePresenter<IShopCartView> {
    public void shopCartGoods(int pageNum, int pageSize) {
        RetrofitUtil.getApi(APIService.class)
                .shopCartGoods(SPUtils.getInstance().getString(SPKey.TOKEN), SPUtils.getInstance().getLong(SPKey.ID), pageNum,pageSize)
                .compose(new NetWorkTransformer<ShopCartGoods>())
                .compose(new LoadingTransformer<ShopCartGoods>(mContext))
                .subscribe(new SimpleObserver<ShopCartGoods>(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull ShopCartGoods shopCartGoods) {
                        getView().shopCartGoodsSuccess(shopCartGoods);
                    }

                    @Override
                    public void showErrorMsg(Throwable e, String errorMsg) {
                        super.showErrorMsg(e, errorMsg);
                        getView().shopCartGoodsFail();
                    }
                });
    }

    public void deleteGoods(String id, final List<Integer> positions) {
        RetrofitUtil.getApi(APIService.class)
                .deleteGoods(SPUtils.getInstance().getString(SPKey.TOKEN), SPUtils.getInstance().getLong(SPKey.ID),id)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {


                    @Override
                    public void call(@NonNull Object o) {
                        getView().deleteGoodsSuccess(positions);
                    }
                });
    }

    public void goodsIsValid(String goodsList) {
        RetrofitUtil.getApi(APIService.class)
                .goodsIsValid(goodsList)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {


                    @Override
                    public void call(@NonNull Object o) {
                        getView().goodsIsValidSuccess();
                    }

                    @Override
                    public void showErrorMsg(Throwable e, String errorMsg) {
                        super.showErrorMsg(e, errorMsg);
                        getView().goodsIsValidFail();
                    }
                });
    }

    public void freight() {
        RetrofitUtil.getApi(APIService.class)
                .freight()
                .compose(new NetWorkTransformer<Freight>())
                .compose(new LoadingTransformer<Freight>(mContext))
                .subscribe(new SimpleObserver<Freight>(mCompositeDisposable) {

                    @Override
                    public void call(@NonNull Freight freight) {
                        getView().freightSuccess(freight);
                    }


                });
    }
}
